
package com.mengmeizi;

import com.mengmeizi.adapter.TimeSelectorAdapter;
import com.mengmeizi.util.Const;
import com.mengmeizi.util.Tools;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

public class ActSwitchWallpaperTimeList extends Activity implements OnItemClickListener, OnClickListener {

    private EditText edvCustomHours;

    private EditText edvCustomMinutes;

    private ListView listView;

    private LinearLayout llyCustom;

    private int selectedItem;

    private Button btnConfirm;

//    private Button btnCancel;

    // private boolean pass;

    private String hoursString;

    private String minutesString;

    private SharedPreferences preferences;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_switchwallpaper_timelist);

        selectedItem = getIntent().getIntExtra(Const.SELECTED_ITEM, 0);

        setTitle("请选择时间");

        preferences = PreferenceManager.getDefaultSharedPreferences(this);

        listView = (ListView) findViewById(R.id.lsvTimes);

        listView.setAdapter(new TimeSelectorAdapter(this));
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 设置单选模式
        listView.setOnItemClickListener(this);

        // 自定义
        llyCustom = (LinearLayout) findViewById(R.id.llyCustom);
        edvCustomHours = (EditText) findViewById(R.id.edvCustomHours);
        edvCustomMinutes = (EditText) findViewById(R.id.edvCustomMinutes);

        if (selectedItem > 4) {
            listView.setItemChecked(5, true);
            hoursString = preferences.getString("hour", "");
            minutesString = preferences.getString("minute", "");
            edvCustomHours.setText(hoursString);
            edvCustomMinutes.setText(minutesString);
            setEditView(true);
        } else {
            listView.setItemChecked(selectedItem, true);
        }

        btnConfirm = (Button) findViewById(R.id.btnConfirm);
        btnConfirm.setOnClickListener(this);
//        btnCancel = (Button) findViewById(R.id.btnCancel);
//        btnCancel.setOnClickListener(this);

    }

    /** ======================listview监听====================== */

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectedItem = position;
        // pass = true;
        if (position == 5) {
            setEditView(true);
        } else {
            setEditView(false);
        }
    }

    public void setEditView(boolean checked) {
        if (checked) {
            edvCustomHours.setEnabled(true);
            edvCustomMinutes.setEnabled(true);
            llyCustom.setBackgroundColor(0xffffffff);
        } else {
            edvCustomHours.setEnabled(false);
            edvCustomMinutes.setEnabled(false);
            llyCustom.setBackgroundColor(0xffeeeeee);
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == btnConfirm) {
            if (selectedItem > 4) {
                int hours = 0;
                int minutes = 0;
                hoursString = edvCustomHours.getText().toString();
                minutesString = edvCustomMinutes.getText().toString();
                if (hoursString.equals("") && minutesString.equals("")) {
                    // 输入时间错误，不设置时间
                    Tools.showShortToast(this, getString(R.string.inputTimeError));
                } else {
                    if (!hoursString.equals("")) {
                        hours = Integer.parseInt(edvCustomHours.getText().toString());
                    }
                    if (!minutesString.equals("")) {
                        minutes = Integer.parseInt(edvCustomMinutes.getText().toString());
                    }
                    if (minutes > 59 || (hours == 0 && minutes == 0)) {
                        Tools.showShortToast(this, getString(R.string.inputTimeError));
                    } else {
                        selectedItem = hours * 36000000 + minutes * 60000;
                        setTime(selectedItem);
                        // 保存
                        Editor editor = preferences.edit();
                        editor.putString("hour", hoursString);
                        editor.putString("minute", minutesString);
                        editor.commit();
                    }
                }
            } else {
                setTime(selectedItem);
            }
//        } else if (v == btnCancel) {
//            this.finish();
        }

    }

    private void setTime(int timeItem) {
        Intent intent = new Intent(this, ActSwitchWallpaper.class);
        intent.putExtra(Const.SELECTED_ITEM, timeItem);
        startActivity(intent);
        this.finish();
    }

}
